package com.bases {
	import com.Event.CheckSelectEvent;
	import com.Event.RelayOutEvent;
	import com.Interface.ICheckBoxCellRender;
	import com.Interface.IItem;
	import com.Interface.IList;
	import com.Interface.IRadioData;

	/**
	 * 多选框列表
	 * @author pethan
	 *
	 */
	public class TCheckButtonList extends TUIComponent implements IList {
		private var checkList:MyList;

		public function TCheckButtonList(){
			super();
			checkList = new MyList();
			checkList.item = TCheckBoxCellRender; /*默认的复选框+文本CellRedener*/
			this.addChild(checkList);
			this.addEventListener(RelayOutEvent.RELAY_OUT_EVENT, onRelayout);
		}

		//重新排序
		public function onRelayout(event:RelayOutEvent):void {
			this.checkList.invalist();
		}

		public function set item(value:Class):void {
			this.checkList.item = value;
		}

		public function removeAllItem():void {
			this.checkList.removeAllItem();
		}

		public function invalist():void {
			this.checkList.invalist();
		}

		public function set col(value:int):void {
			this.checkList.col = value;
			this.checkList.invalist();
		}

		public function addItem(value:*, needRelayout:Boolean = true, isUnshift:Boolean = false):void {
			this.checkList.addItem(value);
		}

		override protected function relayout():void {
			checkList.setSize(200, 200);
		}

		override public function setSize(w:Number, h:Number):void {
			checkList.setSize(w, h);
		}

		private function noSelect(item:*, index:int, arr:Array):void {
			(item as ICheckBoxCellRender).select = false;
		}

		/**
		 *获取被选择的字符
		 */
		public function getSelectText():String {
			var ret:String = '';
			for (var i:int = 0; i < checkList.items.length; i++){
				if ((checkList.items[i] as ICheckBoxCellRender).select){
					ret = ((checkList.items[i] as IItem).userData as IRadioData).getText();
				}
			}
			return ret;
		}

		/**
		 * 获取被选中的编号
		 * @param spilt 按这个进行合并,中间加上spilt
		 * @return
		 *
		 */
		public function getSelectID(spilt:String = ""):String {
			var ret:String = '';
			for (var i:int = 0; i < checkList.items.length; i++){
				if ((checkList.items[i] as ICheckBoxCellRender).select){ /*所有的都要加上;分号进行分隔*/
					ret += ((checkList.items[i] as IItem).userData as IRadioData).getID() + spilt;
				}
			}
			return ret;
		}

		/**
		 * 设置默认选中项
		 * @param index
		 *
		 */
		public function set defaultIndex(index:int):void {
			checkList.items.forEach(noSelect);
			if (this.checkList.items.length != 0){
				(checkList.items[index] as ICheckBoxCellRender).select = true;
			}
		}

		/**
		 * 找到相同的,设为可选
		 * @param text
		 *
		 */
		public function set selectText(text:String):void {
			checkList.items.forEach(noSelect);
			var isSet:Boolean = false;
			for (var i:int = 0; i < checkList.items.length; i++){
				if (((checkList.items[i] as IItem).userData as IRadioData).getText() == text){
					(checkList.items[i] as ICheckBoxCellRender).select = true;
					isSet = true;
				}
			}
			if (!isSet){
				this.defaultIndex = 0;
			}
		}

		public function get counts():int {
			return this.checkList.items.length;
		}
	}
}